package com.soulkun.smarthome.ui.main.personal

import android.graphics.PorterDuff
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainPersonalMessageBinding
import com.soulkun.smarthome.ui.main.MainNavigationDataBindingFragment
import soulkun.library.utils.screen.ScreenUtils
import soulkun.library.utils.toPx


class AppMainPersonalMessageFragment :
    MainNavigationDataBindingFragment<AppFragmentMainPersonalMessageBinding>(
        false
    ) {

    private val viewPager2Adapter by lazy {
        object : FragmentStateAdapter(childFragmentManager, lifecycle) {

            override fun getItemCount(): Int {
                return 2
            }

            override fun createFragment(position: Int): Fragment {
                return if (position == 0) {
                    AppMainPersonalMessageAlertFragment()
                } else {
                    AppMainPersonalMessageFamilyFragment()
                }
            }

        }
    }

    private val tabLayoutMediator by lazy {
        TabLayoutMediator(
            requireDataBinding().tlMainPersonalMessageLayoutTab,
            requireDataBinding().vp2MainPersonalMessageContent
        ) { tab, position ->
            val imageView = ImageView(mContext)
            val tabIconColor: Int =
                ContextCompat.getColor(mContext, R.color.app_gray_C8)
            if (position == 0) {
                imageView.setImageDrawable(
                    ResourcesCompat.getDrawable(
                        resources,
                        R.drawable.app_main_personal_message_icon_alert,
                        null
                    )
                )
                imageView.drawable.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN)
            } else {
                imageView.setImageDrawable(
                    ResourcesCompat.getDrawable(
                        resources,
                        R.drawable.app_main_personal_message_icon_family,
                        null
                    )
                )
                imageView.drawable.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN)
            }
            tab.customView = imageView
            imageView.layoutParams.width = requireContext().resources.getDimensionPixelSize(R.dimen.sw_24dp)
            imageView.layoutParams.height = requireContext().resources.getDimensionPixelSize(R.dimen.sw_24dp)
        }
    }

    private val onTabSelectedListener by lazy {
        object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val tabIconColor: Int =
                    ContextCompat.getColor(mContext, R.color.app_orange_3C)
                val imageView = tab.customView as ImageView
                imageView.drawable.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                val tabIconColor: Int =
                    ContextCompat.getColor(mContext, R.color.app_gray_C8)
                val imageView = tab.customView as ImageView
                imageView.drawable.setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN)
            }

            override fun onTabReselected(tab: TabLayout.Tab) {
            }

        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_personal_message
    }

    override fun initView() {
        super.initView()
        ScreenUtils.setViewFitSystemWindows(requireDataBinding().root)
        requireDataBinding().tlMainPersonalMessageLayoutTab.addOnTabSelectedListener(
            onTabSelectedListener
        )
        requireDataBinding().vp2MainPersonalMessageContent.offscreenPageLimit =
            ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        requireDataBinding().vp2MainPersonalMessageContent.adapter = viewPager2Adapter
        tabLayoutMediator.attach()
    }

    override fun initData() {
        requireDataBinding().onClickListener = View.OnClickListener {
            when(it.id){
                R.id.iv_main_personal_message_icon_back -> {
                    findNavController().navigateUp()
                }
            }
        }
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

    override fun onDestroyView() {
        tabLayoutMediator.detach()
        requireDataBinding().tlMainPersonalMessageLayoutTab.removeOnTabSelectedListener(
            onTabSelectedListener
        )
        super.onDestroyView()
    }

}